/**
 * @file      env_test.cpp
 * @author    yangjian(1171267147@qq.com)
 * @brief     测试opencv环境--打开一个视频文件并在窗口中显示
 * @date      2023-11-04
 *
 * @copyright Copyright (c) 2023
 */
#include <iostream>
#include <opencv2/opencv.hpp>

#include "env.h"

int main(int argc, char** argv) {
    (void)argc;
    (void)argv;

    using std::cout;
    using std::endl;

    cout << "OpenCV version: " << CV_VERSION << '\n';

    auto             file = kResourcesDir + "test01.mp4";
    cv::VideoCapture cap(file);
    /***
     * [ WARN:0@0.009] global cap_gstreamer.cpp:1426 open OpenCV | GStreamer
     * warning: Error opening bin: syntax error [ WARN:0@0.009] global
     * cap_gstreamer.cpp:1164 isPipelinePlaying OpenCV | GStreamer warning:
     * GStreamer: pipeline have not been created
     * @error: 使用“~”描述用户路径，导致gstreamer报错
     */

    if (!cap.isOpened()) {
        cout << "Cannot open video file" << '\n';
        return -1;
    }

    // 输出cap的视频信息
    cout << "Frame width: " << cap.get(cv::CAP_PROP_FRAME_WIDTH) << '\n';
    cout << "Frame height: " << cap.get(cv::CAP_PROP_FRAME_HEIGHT) << '\n';
    cout << "Frame count: " << cap.get(cv::CAP_PROP_FRAME_COUNT) << '\n';
    cout << "FPS: " << cap.get(cv::CAP_PROP_FPS) << '\n';

    auto sleepAsMsec = static_cast<int>(1e3 / cap.get(cv::CAP_PROP_FPS));
    cout << "Sleep time: " << sleepAsMsec << " us" << '\n';

    cv::namedWindow("Video");

    do {
        cv::Mat image;
        cap >> image;
        if (image.empty()) {
            cout << "End of video file" << '\n';
            break;
        }

        imshow("Video", image);

        auto key = cv::waitKey(sleepAsMsec);
        if (key == 27 /*ESC*/ || key == 'q' || key == 'Q') {
            break;
        }
    } while (true);

    cv::destroyAllWindows();

    return 0;
}
